package com.wy.allthingdone.activity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

import com.wy.allthingdone.R;
import com.wy.allthingdone.utils.MusicPlayService;

import java.text.SimpleDateFormat;

public class MusicPlayActivity extends BaseActivity implements View.OnClickListener {

    private ServiceConnection sc = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            musicPlayService = ((MusicPlayService.MyBinder) iBinder).getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            musicPlayService = null;
        }
    };
    private MusicPlayService musicPlayService;
    public android.os.Handler handler = new android.os.Handler();
    private SeekBar seekBar;
    private SimpleDateFormat time = new SimpleDateFormat("m:ss");
    private TextView musicStatus, musicTime;
    private Button btnPlayOrPause, btnStop, btnQuit;
    public Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (musicPlayService.mp.isPlaying()) {
                musicStatus.setText(getResources().getString(R.string.playing));
                btnPlayOrPause.setText(getResources().getString(R.string.pause).toUpperCase());
            } else {
                musicStatus.setText(getResources().getString(R.string.pause));
                btnPlayOrPause.setText(getResources().getString(R.string.play).toUpperCase());
            }
            musicTime.setText(time.format(musicPlayService.mp.getCurrentPosition()) + "/"
                    + time.format(musicPlayService.mp.getDuration()));
            seekBar.setProgress(musicPlayService.mp.getCurrentPosition());
            seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    if (fromUser) {
                        musicPlayService.mp.seekTo(seekBar.getProgress());
                    }
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {

                }
            });
            handler.postDelayed(runnable, 100);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_play);

        musicPlayService = new MusicPlayService(getIntent().getStringExtra("data"));
        bindServiceConection();
        seekBar = (SeekBar) this.findViewById(R.id.MusicSeekBar);
        seekBar.setProgress(musicPlayService.mp.getCurrentPosition());
        seekBar.setMax(musicPlayService.mp.getDuration());

        musicStatus = (TextView) this.findViewById(R.id.MusicStatus);
        musicTime = (TextView) this.findViewById(R.id.MusicTime);

        btnPlayOrPause = (Button) this.findViewById(R.id.BtnPlayorPause);
    }

    private void bindServiceConection() {
        Intent intent = new Intent(MusicPlayActivity.this, MusicPlayActivity.class);
        startService(intent);
        bindService(intent, sc, this.BIND_AUTO_CREATE);
    }

    @Override
    protected void onResume() {
        if (musicPlayService.mp.isPlaying()) {
            musicStatus.setText(getResources().getString(R.string.playing));
        } else {
            musicStatus.setText(getResources().getString(R.string.pause));
        }

        seekBar.setProgress(musicPlayService.mp.getCurrentPosition());
        seekBar.setMax(musicPlayService.mp.getDuration());
        handler.post(runnable);
        super.onResume();
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.BtnPlayorPause:
                musicPlayService.playOrPause();
                break;
            case R.id.BtnStop:
                musicPlayService.stop();
                seekBar.setProgress(0);
                break;
            case R.id.BtnQuit:
                handler.removeCallbacks(runnable);
                unbindService(sc);
                finish();
                break;
            case R.id.btnPre:
                musicPlayService.preMusic();
                break;
            case R.id.btnNext:
                musicPlayService.nextMusic();
                break;
            default:
                break;
        }
    }

    @Override
    public void onDestroy() {
        unbindService(sc);
        super.onDestroy();
    }
}
